package controllers

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"mon-go-web/internal/models"
	"mon-go-web/internal/services"
	"mon-go-web/pkg/logger"
	"mon-go-web/pkg/response"
)

type UserController struct {
	userService *services.UserService
}

func NewUserController() *UserController {
	return &UserController{userService: services.NewUserService()}
}

func (c *UserController) Create(ctx *gin.Context, user *models.User) {
	logger.Logger.Info("Creating user", zap.Any("user", user))
	rows := c.userService.Create(user)
	response.Success(ctx, rows)
}

func (c *UserController) GetByQuery(ctx *gin.Context, query *models.User) {
	logger.Logger.Info("Users retrieved by query", zap.Any("query", query))
	users := c.userService.GetAll()
	response.Success(ctx, users)
}

func (c *UserController) Login(ctx *gin.Context, user *models.User) {
	logger.Logger.Info("Login user", zap.Any("user", user))
	var ok = user.Username == "admin" && user.Password == "123456"
	if !ok {
		response.Fail(ctx, "用户名或密码错误")
		return
	}
	response.Success(ctx, "登录成功!")
}
